﻿jQuery(function($){
	$('.class_top_rowleft a img').each(function(date){
	 	var timg = $('.class_top_rowleft a img').eq(date);
	 		if(timg.attr('src')==""){
	 				timg.css("margin-left","0");
	 				$('td .class_top_rowright a').eq(date).css('line-height','0');
	 				$('.class_top_row table').eq(date).css('overflow','hidden');
	 				$('.class_top_row table tbody tr').eq(date).css('height','14px');
	 		    	$('.class_top_row  table').eq(date).css('background-color','red');
	 			}
	 	});
	 	/*添加广告 begin*/
	// $('#zxadArea').before('<div class="zxw_indexad"> <a href="../../idigger.allyes.com/main/c@db=idigger_2Fbid=53869,25967,1_2Fcid=23875,18697,1342_2Fsid=53007_2Fshow=ignore_2Furl=http_3A_2F_2Fwap.buick.com.cn"> <img src="../images/index_ad.jpg" /> </a> </div>');
	/*添加广告 end*/
	var slideWidth = slideWidth || 280;
	var basePath = '';
	//获取会员登录信息
	var cookieuserinfo = $.cookie('userInfo');
	if(cookieuserinfo)
	{
		var $userAreaDiv = $('.footarclist .box');
		$('span', $userAreaDiv).html(cookieuserinfo.userName);//加载用户名
		$userAreaDiv.css('display', 'block');//显示已登录信息
		$('.loadbtn').css('display', 'none');//隐藏登录、注册按钮
		$('a', $userAreaDiv).bind('click', function(){
			if(confirm('您确定要退出登录吗？'))
			{
				//删除cookie信息
				$.cookie('userInfo',null);
				//滚动页面到顶部
				window.scrollTo(0, 1);
				//弹出层提示已退出
				alert('您已经退出登录');
				$('span', $userAreaDiv).html('');
				$userAreaDiv.css('display', 'none');//隐藏已登录信息
				$('.loadbtn').css('display', 'inline-block');//显示登录、注册按钮
				$('a', $userAreaDiv).unbind();//接触绑定事件
			}
			return false;//阻止默认浏览器点击事件
		});
	}
	$('.gotoTOP').bind('click', function(){
		window.scrollTo(0, 0);
	});
	$('.arclist>a').bind('click',function(){
		$(this).addClass('active');
	});
	//初始化滚动部分
	if(topic_img_num>0){
        topic_img_slide = new slide();
        $(".paging .prev").get(0).addEventListener("click",function(){topic_img_slide.prevPage();},false);
        $(".paging .next").get(0).addEventListener("click",function(){topic_img_slide.nextPage();},false);
        topic_img_slide.init(img_touch);
   }
	if($("#img_ad").attr('src')==""){
			$("#img_ad").css('display','none');
		}
	for (var v=0;v<$('.adimg2 li >a').length;v++)
	{
		var adimgStr=$('.adimg2 li >a').eq(v).text();	
		if (adimgStr.length > 17)
		{
			$('.adimg2 li >a').eq(v).text(getByteVal(adimgStr, 34));
		}
	}
	for (var s=0;s<$('#smallImgList li >a').length;s++)
	{
		var adimgStr1=$('#smallImgList li >a').eq(s).text();	
		if (adimgStr1.length > 8)
		{
			$('.adimg li >a').eq(s).text(adimgStr1);
		}
	}
function getByteVal(val, max) {
            var returnValue = '';
            var byteValLen = 0;
            for (var i = 0; i < val.length; i++) {
                if (val[i].match(/[^\x00-\xff]/ig) != null)
                    byteValLen += 2;
                else
                    byteValLen += 1;
                if (byteValLen > max)
                    break;
                returnValue += val[i];
            }
            return returnValue;
        }
	var uuu=location.href;
	var isJUC = navigator.userAgent.toLowerCase().indexOf('juc')
	try
	{
		if (isJUC >= 0)
		{
			if (uuu.toLowerCase().indexOf('/iphone') >=0)
			{
				var uurl=uuu.replace('../iphone','../common');
				window.location.href=uurl;
			}
		}
	}
	catch(e){}

$.get('../iphone/new_index/ad/index_adv/indexadvtop/index.xml', function(data){
var strHtml = '';
$(data).find('addoc').each(function(i){
var name = $(this).find('name').text();
var apic = $(this).find('apic').text();
var ico  = $(this).find('ico').text();

if (ico.substring(1,ico.length)=="")
{
strHtml += " <p> \n" + name.substring(1,name.length) + "\n </p> \n";
}
else
{
strHtml += " <p> \n" + apic.substring(1,apic.length) + "\n </p> \n";	
}

})       
$('#zxadArea').html(strHtml);
});       
      

$("#ydb").bind('click',function(){
									if($("#topydul").css("display") =="block"){
											$("#topydul").css("display","none");
											return;
										}
									if($("#topydul").css("display") =="none"){
											$("#topydul").css("display","block");
											return;
										}
									});
});
var slidepagenum =4;
function slide() {
  var _currentX = 0;
  var slideNum = 0;
  var self = this;
  this.init = function(g_touch) {
    this.inContainer = $("#" + g_touch.swapId);
    slideNum = g_touch.slideNum || 0;
    if (g_touch.itemWidth) this.slideWidth = g_touch.itemWidth;
    else this.slideWidth = parseInt(this.inContainer.css("width")) / slideNum;
    if (g_touch.marginValue) this.addV = g_touch.marginValue;
    else this.addV = 0;
    this._currentX = 0;
    this.viewIndex = 0;
    this.currClick = 0;
    this.pageSlot = 0;
    this.duration = g_touch.duration || "400ms";
    this.carousel = true;
    if (g_touch.callBackFunc == "") this.callback = null;
    else this.callback = g_touch.callBackFunc + "()";
    this.isCycle = g_touch.isCycle || false;
    this.isDoclick = g_touch.isDoclick || true;
    this.imglist = g_touch.imglist || null;
    this.tempImagList = [];
    this.callBackDoclick = null;
    if (this.isDoclick) {
      if (g_touch.callBackDoclick != "" || typeof(g_touch.callBackDoclick) != "undefined") this.callBackDoclick = g_touch.callBackDoclick + "()";
    }
    this.preloadArr = new Array();
    if (this.imglist) {
      for (var i = 0; i < slideNum; i++) {
        if (i == 0) {
          this.preloadArr.push(true);
        }
        else this.preloadArr.push(false);
      }
    }
    this.inContainer.css("-webkit-transition-timing-function", "ease-out");
    this.inContainer.css("-webkit-transform", "translate3d(0,0,0)");
    this.inContainer.css("-webkit-transition-property", "-webkit-transform");
    this.inContainer.css("-webkit-transition-delay", 0);
    this.inContainer.css("-moz-transition-timing-function", "ease-out");
    this.inContainer.css("-moz-transition-property", "-moz-transform");
    this.inContainer.css("-moz-transform", "translate3d(0,0,0)");
    this.inContainer.css("-moz-transition-delay", 0);
    if (slideNum < 3) {
      this.carousel = false;
      this.isCycle = false;
    }
    if (this.isCycle) {
      this.arrangePages()
    }
    this.inContainer.get(0).addEventListener("touchstart",
    function() {
      self.touchStart()
    },
    false);
    this.inContainer.get(0).addEventListener("touchmove",
    function() {
      self.touchMove()
    },
    false);
    this.inContainer.get(0).addEventListener("touchend",
    function() {
      self.touchEnd()
    },
    false);
    this.inContainer.get(0).addEventListener("webkitTransitionEnd",
    function() {
      if (self.isCycle) {
        self.arrangePages();
      }
    },
    false);
  };
  /*下一页*/
  this.nextPage = function() {

    if (this.carousel) {
      if (this.viewIndex == slideNum - 1) {
        if (this.isCycle) this.setPage(0, this.pageSlot + 1)
      } else {
		  	
				if(this.callback=="topicSlideCallback()"){
					var topic_img_num =Math.ceil($('#top_scllerdiv #columns li').length / slidepagenum);
					 if(this.viewIndex == topic_img_num -2){
						 	$(".next").css("background-position",'-59px -435px');
						 }
					  	$(".prev").css("background-position",'-23.5px  -435px');
					}
		
        this.setPage(this.viewIndex + 1, this.pageSlot + 1)
      }
    }
    else {
      this.setPage(Math.min(this.viewIndex + 1, slideNum - 1))
    }
    this.slideAction(this.pageSlot, this.duration);
  }
  /*上一页*/
  this.prevPage = function() {
    if (this.viewIndex == 0) {
		
      if (this.isCycle) this.setPage(slideNum - 1, this.pageSlot - 1)
    } else {
			if(this.callback=="topicSlideCallback()"){
		
				if(this.viewIndex ==1){
						$(".prev").css("background-position",'-59px -435px');
					}
					var topic_img_num =Math.ceil($('#top_scllerdiv #columns li').length / slidepagenum);
				if( topic_img_num>1){
						$(".next").css("background-position",'8px -435px');
					}
			}
      this.setPage(this.viewIndex - 1, this.pageSlot - 1)
    }
    this.slideAction(this.pageSlot, this.duration);
  }
  this.setPage = function(pageNum, a) {
    this.viewIndex = pageNum;
    this.pageSlot = a != null ? a: this.viewIndex
    eval(this.callback);
  }
  this.slideAction = function(d, t) {
    if (d == 0) {
      if (!this.isCycle) a = 0
      else a = this.addV
    }
    else a = -d * this.slideWidth + this.addV;
    var duration = !t ? "0": t
    this.cssTranslation(a, t);
  };
  this.cssTranslation = function(a, t) {
    if (this._currentX == a && this.isCycle) {
      this.arrangePages();
      return
    }
    this.setTranslation(a);
    this.inContainer.css("webkitTransform", 'translate3d(' + a + 'px,0px,0px)');
    this.inContainer.css("-webkitTransitionDuration", t);
    
    this.inContainer.css("-moz-transform", 'translateX(' + a + 'px)');
    this.inContainer.css("-moz-transition-duration", t);
  }
  this.setChildTranslation = function(b, a) {
    if (b._currentX == a) {
      return
    }
    b._currentX = a;
    b.css("webkitTransform", 'translate3d(' + a + 'px,0px,0px)')
  };
  this.setTranslation = function(v) {
    this._currentX = v;
  };
  this.arrangePages = function() {
    if (this.carousel) {
      var d = this.getTranslation();
      if (this.viewIndex == 0) d = d - this.addV;
      var h = this.slideWidth,
      b = slideNum,
      l = -1 * Math.floor(d / h),
      f = (b + (l % b)) % b,
      i = h * b,
      c = -1 * Math.ceil(d / i),
      e = Math.ceil((b - 1) / 2),
      g = Math.floor((b - 1) / 2),
      a,
      k;
      carouselBufferLimit = 4;
      if (carouselBufferLimit > 0) {
        e = Math.min(e, carouselBufferLimit);
        g = Math.min(g, carouselBufferLimit)
      }
      k = i * c;
      this.setChildTranslation(this.inContainer.find("li").eq(f), k);
      while (e > 0) {
        a = (f + e) % b;
        k = a < f ? i * (c + 1) : i * c;
        this.setChildTranslation(this.inContainer.find("li").eq(a), k); --e
      }
      while (g > 0) {
        a = (b + (f - g)) % b;
        k = a > f ? i * (c - 1) : i * c;
        this.setChildTranslation(this.inContainer.find("li").eq(a), k); --g
      }
    }
  };
  this.getTranslation = function() {
    return this._currentX;
  }
  this.touchStart = function() {
    this.isMoving = false;
    var currTouch = event.touches[0];
    this.currX = currTouch.screenX;
    this.currY = currTouch.screenY;
    this.currDeltaX = this.getTranslation();
    this.DeltaX = 0;
    var _self = self;
    if (this.isCycle) self.arrangePages();
  };
  this.touchMove = function() {
    var b = event.touches[0].screenX;
    this.DeltaX = b - this.currX;
    this.DeltaY = event.touches[0].screenY - this.currY;
    if (!this.isMoving) {
      this.doScrollX = Math.abs(this.DeltaX) > Math.abs(this.DeltaY);
      this.isMoving = true;
      if (this.doScrollX) {
        event.preventDefault();
      }
    }
    else {
      if (this.doScrollX) {
        var curScrollX = parseInt(this.currDeltaX) + parseInt(this.DeltaX);
        this.cssTranslation(curScrollX, "0s")
        event.preventDefault();
      }
    }
  };
  this.touchEnd = function() {
    var moveX = this.DeltaX;
    if (this.isMoving) {
      if (self.isDoclick) self.inContainer.get(0).removeEventListener("click", self.doClick, false);
      if (this.doScrollX) {
        if (moveX > 10) {
          self.prevPage();
        } else if (moveX < -10) {
          self.nextPage();
        }
        else {
          self.slideAction(this.pageSlot, "0s");
        }
      }
      else {
        self.slideAction(this.pageSlot, "0s");
      }
    }
    else {
      if (self.isDoclick) {
        self.doClick();
      }
    }
    self.inContainer.get(0).removeEventListener("touchmove",
    function() {
      _self.touchMove()
    },
    false);
    self.inContainer.get(0).removeEventListener("touchend",
    function() {
      _self.touchEnd()
    },
    false);
  };
  this.gotoItemByIndex = function(index) {
    this.setPage(index);
    this.slideAction(this.pageSlot, "0s");
  };
}
var staticPath = '';
var g_app = {
         topic_img_div:"columns"
};
var g_data = {
	"info":[]
};
/*首页导航滚动*/
$(document).ready(function() {
	var topic_img_num =Math.ceil($('#top_scllerdiv #columns li').length / slidepagenum);
	if(topic_img_num <= 1){
			$(".next").css("background-position",'-59px -435px');
		}
		$(".prev").css("background-position",'-59px -435px');
});
var topic_img_num =Math.ceil($('#columns li').length / slidepagenum);
var img_touch = {swapId:g_app.topic_img_div,
   slideNum:topic_img_num,
   duration:"400ms",
   isCycle:false,
   isDoclick:true,
   imglist:g_data.info, 
   callBackFunc:"topicSlideCallback",
   callBackDoclick:"imgdoclickCallback"
};
function topicSlideCallback_big()
{
	if(big_img_num>0)
	{
		document.querySelector('#big_page_img li.actived').className = '';
		document.querySelector('#big_page_img li:nth-child(' + (big_img_slide.viewIndex+1) + ')').className = 'actived';
	}
}
function topicSlideCallback(){
}
function imgdoclickCallback(){
}

